﻿using ERPPublic;
using MediatR;
using Newtonsoft.Json;
using SqlSugar;
using WarehouseManagement.Application.Command.OutboundShipmentsCommand;
using WarehouseManagement.Domain.DTO;
using WarehouseManagement.Infrstructre.Interface;

namespace WarehouseManagement.Application.CommandHandler.OutboundShipmentsCommandHandlers
{
    public class OutboundShipmentsDeleteCommandHandlers : IRequestHandler<OutboundShipmentsDeleteCommand, int>
    {
        protected readonly IOutboundRequestRepository outbound;// 出库请求仓储接口
        protected readonly IOutboundRProductRepository outboundR; // 仓库出库申请产品关系仓储接口
        protected readonly ISqlSugarClient db;

        public OutboundShipmentsDeleteCommandHandlers(IOutboundRequestRepository outbound, IOutboundRProductRepository outboundR, ISqlSugarClient db)
        {
            this.outbound = outbound;
            this.outboundR = outboundR;
            this.db = db;
        }

        public async Task<int> Handle(OutboundShipmentsDeleteCommand request, CancellationToken cancellationToken)
        {
            var code = 0;
            foreach (var item in request.ids)
            {
                var newOutbound = (await outbound.GetAll()).First(x => x.ManpowerId == item);
                if (newOutbound != null)
                {
                    newOutbound.IsDel = true;
                    newOutbound.DelName = request.DelName;
                    newOutbound.DelTime = DateTime.Now;
                    await outbound.UpdateAsync(newOutbound);
                    MessageCreateDto dto = new MessageCreateDto();
                    dto.MType = "出库申请";
                    dto.Title = "出库申请删除";
                    dto.SendTime = DateTime.Now;
                    await MessageCreates(dto);
                    code = 1;
                }
                else
                {
                    code = -1;
                }
            }
            return code;
        }
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="dtos"></param>
        /// <returns></returns>
        public async Task<int> MessageCreates(MessageCreateDto dtos)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1MCIsInNpZCI6IjE4NjcwOTU3MzQ0NjIwNTg0OTYiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMTksMTkiLCJqdGkiOiI1MGYyMWRmZS0yNWQ3LTQ1OTQtOTg1Yi0wZTM2MDhhZjJiNmYiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5LuY5rSb5ZiJIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0._wh23n1M7asMGST9NyeZj-5rk5ICAiOFv6yDk6e2o7Y";
            HttpClientWrapper cilent = new HttpClientWrapper(token);
            string chanpin = "http://116.205.121.197:8000/erpcp/api/Message/SendMessage";
            string jsonstr = JsonConvert.SerializeObject(dtos);
            string postReponse = await cilent.PostAsync(chanpin, jsonstr);
            return 1;
        }
    }
}
